package net.roobook.common.configure;

import net.roobook.common.interceptor.RoobookServerProtectInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author Roobook
 * @description 微服务防护配置类，使RoobookServerProtectInterceptor拦截器生效
 * @date 2022/4/8
 */
public class RoobookServerProtectConfigure implements WebMvcConfigurer {

    @Bean
    // 表示当IOC容器里没有PasswordEncoder类型的Bean的时候，则将BCryptPasswordEncoder注册到IOC容器中
    @ConditionalOnMissingBean(value = PasswordEncoder.class)
    // 密码相关，对于一个相同的密码，每次加密出来的加密串都不同 net.roobook.auth.configure.PasswordEncoderTest
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public HandlerInterceptor roobookServerProtectInterceptor() {
        return new RoobookServerProtectInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(roobookServerProtectInterceptor());
    }
}